﻿using System;
using System.Web;
using System.Collections;
using System.ComponentModel;
using System.Web.SessionState;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web.Security;
using LTP.Accounts.Bus;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
namespace jnlamp.Web 
{
	/// <summary>
	/// Global 的摘要说明。
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.IContainer components = null;
        static public System.Timers.Timer myTimer = new System.Timers.Timer();
        public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>                                                                             
        /// 定时器初始化                                                                          
        /// </summary>                                                                            
        private void sysTimerInit()
        {
            myTimer.Interval = 1000 * 60 * 60 * 1;//1个小时1000 * 60;// 
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
            myTimer.AutoReset = true;
            myTimer.Enabled = true;
            //开始启动
            myTimer.Start();
        }

        /// <summary>                                                                             
        /// 定时任务事件委托                                                                      
        /// </summary>                                                                            
        /// <param name="sender"></param>                                                         
        /// <param name="e"></param>                                                              
        private void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //执行开始或结束日期     
            //执行开始或结束日期
            string tbcbdtime = ConfigurationManager.AppSettings["tbcbdtime"];
            BLL.prouserinfo bll = new BLL.prouserinfo();
            string sms = ConfigurationManager.AppSettings["sendsms"];
            string phone = "";
            DataSet ds;
            DataSet dsyj;
            DateTime dtnow = DateTime.Now;
            int uid = 0;
            if (tbcbdtime != null && tbcbdtime != "-1")
            {
                int cbd = 0;
                int.TryParse(tbcbdtime, out cbd);
                if (cbd != 0)
                    if (dtnow.Hour == cbd)
                    {
                        ds = bll.GetListYJphone("");
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                phone = ds.Tables[0].Rows[i]["uphone"].ToString();
                                int.TryParse(ds.Tables[0].Rows[i]["uid"].ToString(), out uid);
                                //取得当前技术人员的所有预警信息
                                dsyj = bll.GetListYJ("uphone='" + phone + "'");
                                string temp = "";
                                for (int j = 0; j < dsyj.Tables[0].Rows.Count; j++)
                                {
                                    if (temp == "")
                                        temp = dsyj.Tables[0].Rows[j]["basename"].ToString();
                                    else
                                        temp += "," + dsyj.Tables[0].Rows[j]["basename"].ToString();
                                }
                                if (temp != "")
                                {
                                    temp = sms.Replace("sms", temp);
                                    int rid = 0;
                                    //APP预警发送
                                    jnlamp.BLL.info blli = new jnlamp.BLL.info();
                                    jnlamp.Model.info model = new jnlamp.Model.info();
                                    model.title = "系统发送";
                                    model.infotime = DateTime.Now;
                                    model.uid = uid;
                                    model.infocontent = temp;
                                    rid = blli.Add(model, uid);
                                    //发送手机信息
                                    if (phone.Length == 11)
                                    {                                        
                                            string strRet = Common.SMSSender.GetHtmlFromUrl(phone, temp);                                            
                                            int.TryParse(strRet, out rid);
                                            if (rid < 0)
                                            {
                                                if (strRet == "-1")
                                                {
                                                    strRet = "没有该用户账户";
                                                }
                                                else if (strRet == "-2")
                                                {
                                                    strRet = "接口密钥不正确";
                                                }
                                                else if (strRet == "-21")
                                                {
                                                    strRet = "MD5接口密钥加密不正确";
                                                }
                                                else if (strRet == "-3")
                                                {
                                                    strRet = "短信数量不足";
                                                }
                                                else if (strRet == "-11")
                                                {
                                                    strRet = "该用户被禁用";
                                                }
                                                else if (strRet == "-14")
                                                {
                                                    strRet = "短信内容出现非法字符";
                                                }
                                                else if (strRet == "-4")
                                                {
                                                    strRet = "手机号格式不正确";
                                                }
                                                else if (strRet == "-41")
                                                {
                                                    strRet = "手机号码为空";
                                                }
                                                else if (strRet == "-42")
                                                {
                                                    strRet = "短信内容为空";
                                                }
                                                else if (strRet == "-51")
                                                {
                                                    strRet = "短信签名格式不正确";
                                                }
                                                else if (strRet == "-6")
                                                {
                                                    strRet = "IP限制";
                                                }
                                                FileStream fs = null;
                                                string filePath = GetRootPath() + "\\sms.txt";
                                                //将待写的入数据从字符串转换为字节数组  
                                                Encoding encoder = Encoding.UTF8;
                                                byte[] bytes = encoder.GetBytes(strRet + phone + ":" + temp + "\r\n");
                                                try
                                                {
                                                    fs = File.OpenWrite(filePath);
                                                    //设定书写的开始位置为文件的末尾  
                                                    fs.Position = fs.Length;
                                                    //将待写入内容追加到文件末尾  
                                                    fs.Write(bytes, 0, bytes.Length);
                                                    
                                                }
                                                catch (Exception ex)
                                                {

                                                }
                                                finally
                                                {
                                                    fs.Close();
                                                }
                                            }
                                       
                                    }
                                }
                            }
                        }
                    }
            }

        }
        public static string GetRootPath()
        {
            string AppPath = "";
            HttpContext HttpCurrent = HttpContext.Current;
            if (HttpCurrent != null)
            {
                AppPath = HttpCurrent.Server.MapPath("~");
            }
            else
            {
                AppPath = AppDomain.CurrentDomain.BaseDirectory;
                if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                    AppPath = AppPath.Substring(0, AppPath.Length - 1);
            }
            return AppPath;
        }                 
		public Global()
		{
			InitializeComponent();
		}	
		
		protected void Application_Start(Object sender, EventArgs e)
		{
            this.sysTimerInit();
           #region 默认蓝
            						
			Application["1xtop1_bgimage"]="images/top-1.gif"; //顶框背景图片
			Application["1xtop2_bgimage"]="images/top-2.gif"; //顶框背景图片
			Application["1xtop3_bgimage"]="images/top-3.gif"; //顶框背景图片
			Application["1xtop4_bgimage"]="images/top-4.gif"; //顶框背景图片
			Application["1xtop5_bgimage"]="images/top-5.gif"; //顶框背景图片
			Application["1xtopbj_bgimage"]="images/top-bj.gif"; //顶框背景图片

			Application["1xtopbar_bgimage"]="images/topbar_01.jpg"; //顶框工具条背景图片
			Application["1xfirstpage_bgimage"]="images/dbsx_01.gif"; //首页背景图片
			Application["1xforumcolor"]="#f0f4fb";
			Application["1xleft_width"]="204"; //左框架宽度
			
			Application["1xtree_bgcolor"]="#e3eeff"; //左框架树背景色
			Application["1xleft1_bgimage"]="images/left-1.gif"; 
			Application["1xleft2_bgimage"]="images/left-2.gif"; 
			Application["1xleft3_bgimage"]="images/left-3.gif"; 
			Application["1xleftbj_bgimage"]="images/left-bj.gif"; 

			Application["1xspliter_color"]="#6B7DDE"; //分隔块色

			Application["1xdesktop_bj"]="";//images/right-bj.gif
			Application["1xdesktop_bgimage"]="images/desktop_01.gif";//right.gif

			Application["1xtable_bgcolor"]="#F5F9FF"; //最外层表格背景
			Application["1xtable_bordercolorlight"]="#4F7FC9"; //中层表格亮边框
			Application["1xtable_bordercolordark"]="#D3D8E0"; //中层表格暗边框
			Application["1xtable_titlebgcolor"]="#E3EFFF"; //中层表格标题栏

			Application["1xform_requestcolor"]="#E78A29"; //表单中必填字段*颜色
			Application["1xfirstpage_topimage"]="images/top_01.gif";
			Application["1xfirstpage_bottomimage"]="images/bottom_01.gif";
			Application["1xfirstpage_middleimage"]="images/bg_01.gif";
			#endregion 		

		}
 
		protected void Session_Start(Object sender, EventArgs e)
		{
			Session["Style"]=1;
		}
		protected void Application_BeginRequest(Object sender, EventArgs e)
		{
		}
		protected void Application_EndRequest(Object sender, EventArgs e)
		{
		}
		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{
		}
        protected void Application_Error(Object sender, EventArgs e)
        {
            Exception lastError = Server.GetLastError();

            if (lastError != null)
            {
                if (lastError is HttpException)
                {
                    Server.ClearError(); // 清除错误状态
                    
                }
                log.Error("Log4错误", lastError);

            }
        }
		protected void Session_End(Object sender, EventArgs e)
		{		
			
		}
		protected void Application_End(Object sender, EventArgs e)
		{
		}
			
		#region Web 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.components = new System.ComponentModel.Container();
		}
		#endregion
	}
}

